package com.example.dell.guidepro0718xubowen.mode0718xubowen;

import com.example.dell.guidepro0718xubowen.Iface.WeatherInface;
import com.example.dell.guidepro0718xubowen.bean.WeatherResult;
import com.example.dell.guidepro0718xubowen.listener.WeatherListener;
import com.example.dell.guidepro0718xubowen.service.Weather51Service;
import com.example.dell.guidepro0718xubowen.service.WeatherService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dell on 2017/3/29.
 */

public class WeatherMode implements WeatherInface {
   private static final String BASE_URL="http://www.weather.com.cn/data/";
   private WeatherService service;
    private Retrofit retrofit;
    @Override
    public void getWeather(String cityNumber, final WeatherListener listener) {
     //步骤1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //步骤2
         service=retrofit.create(WeatherService.class);
        Call<WeatherResult> call=service.getResult(cityNumber);
        //步骤3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
           if (response.isSuccessful() && response.body()!=null){
               listener.onResponse(response.body().getWeatherInfo());
           }
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
           listener.onFailure(t.toString());
            }
        });
    }
}
